//
//  FZHomeViewController.swift
//  FZSina_Swift
//
//  Created by 李忠 on 7/19/16.
//  Copyright © 2016 FZLZ. All rights reserved.
//

import UIKit


enum FZHomeRefreshType: Int {
    
    ///下拉刷新
    case PULL = 0
    ///上啦加载更多
    case PUSH = 1
}

private let homeCellID = "homeCellID"

class FZHomeViewController: FZVisitorViewController {


    override func viewDidLoad() {
        super.viewDidLoad()
        if !isLogin {
            noLoginView.setupVisitorViewInfo(nil, title: nil)
            return
        }
        setupUI()
        setupLayout()
        
        //开始刷新
        pushRefresh.beginRefreshing()
        
    }
    
//MARK:==========================加载界面==========================
    func setupUI()  {
        
        self.tableView.delegate = self
        self.tableView.dataSource = self
        self.tableView.estimatedRowHeight = 100
        self.tableView.registerClass(FZHomeStatusListCell.self, forCellReuseIdentifier: homeCellID)
        self.tableView.separatorStyle = .None
        self.tableView.tableFooterView = indicatorView
        
        //添加刷新
        self.tableView.addSubview(pushRefresh)
        pushRefresh.addTarget(self, action: #selector(benginRefresh), forControlEvents: UIControlEvents.ValueChanged)
        //提示消息条，添加到导航栏的view
        navigationController!.view.insertSubview(noticeView, belowSubview: navigationController!.navigationBar)
    }
    
    func setupLayout() {
        noticeView.snp_makeConstraints { (make) in
            
            make.top.equalTo(39)
            make.leading.trailing.equalTo(0)
            make.height.equalTo(25)
        }
    }


    
    func benginRefresh()  {

        loadListData(.PULL)
    }
    
//MARK:==========================加载数据==========================
    func loadListData(type: FZHomeRefreshType) {
        
        statusListVM.loadStatListData(type) { (newMessages, isComplete) in
            self.endRefresh()
            if(!isComplete){
                
                return
            }
            
            if newMessages >= 0{
                
                if newMessages == 0{
                    
                    self.noticeView.text = "没有更多的消息，么么哒！"
                    self.startNoticeAniamted()
                }else{
                    
                    self.noticeView.text = "没有\(newMessages)条消息，么么哒！"
                    self.startNoticeAniamted()
                }
            }
            self.tableView.reloadData()
        }
    }
    func endRefresh() {
        indicatorView.stopAnimating()
        pushRefresh.endRefreshing()
    }
    
    //MARK: 开始动画
    func startNoticeAniamted() {
        if !noticeView.hidden {
            return
        }
        noticeView.hidden = false
        UIView.animateWithDuration(0.5, animations: {
            self.noticeView.transform = CGAffineTransformMakeTranslation(0, 25)
            }) { (_) in
                UIView.animateWithDuration(0.25, delay: 0.5, options: [], animations: {
                    self.noticeView.transform = CGAffineTransformIdentity
                    }, completion: { (_) in
                        self.noticeView.hidden = true
                })
        }
        
    }
//MARK:==========================懒加载==========================
    lazy var statusListVM: FZHomeStatusListViewModel = FZHomeStatusListViewModel()
    
    lazy var indicatorView: UIActivityIndicatorView = {
        
        let indicator: UIActivityIndicatorView = UIActivityIndicatorView()
        
        indicator.color = ThemColor
        indicator.frame.size.height = 30.0
        
        return indicator
    }()
    //刷新控件
    lazy var  pushRefresh: FZRefreshControl = FZRefreshControl()
    //消息提醒控件
    lazy var noticeView: UILabel = {
        let lb = UILabel(textColor: UIColor.whiteColor(), textFont: 15)
        lb.backgroundColor = ThemColor
        lb.hidden = true
        lb.textAlignment = .Center
        return lb
    }()
}

//MARK:==========================分类==========================
extension FZHomeViewController: UITableViewDelegate, UITableViewDataSource{
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return statusListVM.statusList.count
    }
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCellWithIdentifier(homeCellID, forIndexPath: indexPath) as! FZHomeStatusListCell
        
        cell.status = statusListVM.statusList[indexPath.row]
        
        return cell
    }

    func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        
        if indexPath.row == statusListVM.statusList.count - 1 && !indicatorView.isAnimating() {
        
            indicatorView.startAnimating()
            loadListData(.PUSH)
            
        }
    }
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        tableView.deselectRowAtIndexPath(indexPath, animated: false)
    }
    
}
